package org.glut.backend.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.glut.backend.service.SmsService;
import org.glut.backend.service.VerificationCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: gluglu2
 * @date: 2025/8/5 11:27
 */
@RestController
public class SmsController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private VerificationCodeService verificationCodeService;

//    public SmsController(SmsService smsService,
//                         VerificationCodeService verificationCodeService) {
//        this.smsService = smsService;
//        this.verificationCodeService = verificationCodeService;
//    }

    /***
     * @description: 发送验证码接口
     * @return null
     * @author: gluglu2
     * @date: 2025/8/5 15:16
     */
    @GetMapping("/SmsSend")
    public ResponseEntity<Map<String, Object>> sendVerificationCode(
            @RequestParam String mobile) {
        try {
            // 生成6位随机验证码
            String code = String.format("%06d", (int)(Math.random() * 1000000));
            int minute = 3; // 3分钟有效

            // 存储验证码
            verificationCodeService.storeCode(mobile, code, minute);

            // 发送短信
            String apiResponse = smsService.sendVerificationCode(mobile, code, minute);

            // 解析响应
            JSONObject jsonResponse = JSON.parseObject(apiResponse);
            if ("0".equals(jsonResponse.getString("code"))) {
                return ResponseEntity.ok(Map.of(
                        "success", true,
                        "message", "验证码发送成功",
                        "expireIn", minute * 60
                ));
            } else {
                verificationCodeService.clearCode(mobile); // 发送失败清除验证码
                return ResponseEntity.badRequest().body(Map.of(
                        "success", false,
                        "message", jsonResponse.getString("msg")
                ));
            }

        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Map.of(
                    "success", false,
                    "message", "系统错误: " + e.getMessage()
            ));
        }
    }

    /***
     * @description: 验证码校验接口
     * @param mobile
     * @param code
     * @return org.springframework.http.ResponseEntity<java.util.Map<java.lang.String,java.lang.Object>>
     * @author: gluglu2
     * @date: 2025/8/5 15:20
     */
    @GetMapping("/SmsVerify")
    public ResponseEntity<Map<String, Object>> verifyCode(
            @RequestParam String mobile,
            @RequestParam String code) {

        boolean isValid = verificationCodeService.verifyCode(mobile, code);

        if (isValid) {
            verificationCodeService.clearCode(mobile); // 验证成功后清除
            return ResponseEntity.ok(Map.of(
                    "success", true,
                    "message", "验证码正确"
            ));
        } else {
            return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "验证码错误或已过期"
            ));
        }
    }
}